昨天我們對抓下來的資料解碼了,今天我們要來顯示這些資料。
這裡在顯示的畫面呼叫昨天寫的decoder,然後對之前寫的結構decode,之後就能將資料取出,然後print出來,失敗的話會顯示錯誤訊息。
NetworkManager.shared.getWeatherData(city: chooseCityNameEN) { (response: CurrentWeatherData?) in
guard let name = response?.name else { return }
guard let coordLon = response?.coord.lon else { return }
guard let coordLat = response?.coord.lat else { return }
guard let temp_min = response?.main.temp_min else { return }
guard let temp_max = response?.main.temp_max else { return }
guard let description = response?.weather[0].description else { return }
guard let cityWeather = response?.weather[0].main else { return }
print("============== Weather data ==============")
print(response!)
print("============== Weather data ==============")
print("城市名稱: \(name)")
print("經緯度: (\(coordLon),\(coordLat))")
print("最高溫: \(temp_max)°C, 最低溫: \(temp_min)°C")
print("描述: \(description)")
} failure: { errorMessage in
print(errorMessage!)
}
今天的進度就到這裡啦,明天來加些東西。